﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;

public class SkyDriveConnection : PassportConnection
{
	/// <summary>
	/// Create a new object to handle SkyDrive communiction.
	/// </summary>
	/// <param name="userName"></param>
	/// <param name="password"></param>
	public SkyDriveConnection(string userName, string password)
		: base(userName, password)
	{
	}

	/// <summary>
	/// Fetch all WebDAV folders visible using SkyDrive web service.
	/// </summary>
	/// <returns></returns>
	public SkyDriveFolder[] GetDavFolders()
	{
		string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
			"<s:Body><GetWebAccountInfoRequest xmlns=\"http://schemas.microsoft.com/clouddocuments\">"+
			"<BaseRequest xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"+
			"<ClientAppId>Microsoft Office/14.0 (Windows NT 6.1; Microsoft Word 14.0.4536; Pro)</ClientAppId><Market>en-US</Market>"+
			"<SkyDocsServiceVersion>v1.0</SkyDocsServiceVersion></BaseRequest>"+
			"<GetReadWriteLibrariesOnly>true</GetReadWriteLibrariesOnly></GetWebAccountInfoRequest></s:Body></s:Envelope>";

		HttpWebResponse resp = SendSoapRequest("http://docs.live.net/SkyDocsService.svc", "GetWebAccountInfo", soap);
		XmlDocument doc = new XmlDocument();
		doc.LoadXml(GetResponseData(resp));
		XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
		nsmgr.AddNamespace("ms", "http://schemas.microsoft.com/clouddocuments");
		XmlNodeList list = doc.SelectNodes("//ms:Library", nsmgr);
		SkyDriveFolder[] arr = new SkyDriveFolder[list.Count];
		for (int i = 0; i<list.Count; i++)
		{
			XmlElement el = list[i] as XmlElement;
			SkyDriveFolder folder = new SkyDriveFolder();
			folder.Name = el.SelectSingleNode("ms:DisplayName", nsmgr).InnerText;
			folder.WebDAVUrl = el.SelectSingleNode("ms:DavUrl", nsmgr).InnerText;
			arr[i] = folder;
		}
		return arr;
	}

	/// <summary>
	/// Fetch contents of a single SkyDrive folder, using the WebDAV protocol.
	/// </summary>
	/// <param name="folder"></param>
	/// <returns></returns>
	public SkyDriveItem[] GetDavFolderContent(SkyDriveFolder folder)
	{
		InternalWebRequest spec = new InternalWebRequest("PROPFIND", folder.WebDAVUrl);
		spec.Headers["Depth"] = "1";
		spec.Headers["translate"] = "f";
		HttpWebResponse resp = SendRequest(spec);

		XmlDocument doc = new XmlDocument();
		doc.LoadXml(GetResponseData(resp));
		XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
		nsmgr.AddNamespace("d", "DAV:");
		XmlNodeList list = doc.SelectNodes("//d:response", nsmgr);
		SkyDriveItem[] arr = new SkyDriveItem[list.Count-1];
		for (int i = 1; i < list.Count; i++)
		{
			XmlElement el = list[i] as XmlElement;
			string isFolder = el.SelectSingleNode("d:propstat/d:prop/d:isFolder", nsmgr).InnerText;
			if (isFolder == "t")
			{
				SkyDriveFolder item = new SkyDriveFolder();
				item.Name = el.SelectSingleNode("d:propstat/d:prop/d:displayname", nsmgr).InnerText;
				item.WebDAVUrl = el.SelectSingleNode("d:href", nsmgr).InnerText;
				arr[i - 1] = item;
			}
			else
			{
				SkyDriveFile item = new SkyDriveFile();
				item.Name = el.SelectSingleNode("d:propstat/d:prop/d:displayname", nsmgr).InnerText;
				item.Length = long.Parse(el.SelectSingleNode("d:propstat/d:prop/d:getcontentlength", nsmgr).InnerText);
				item.Modified = DateTime.Parse(el.SelectSingleNode("d:propstat/d:prop/d:getlastmodified", nsmgr).InnerText);
				arr[i - 1] = item;
			}
		}
		return arr;
	}
}
